package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.DesignCollectDAO;
import com.proactive.webapp.entity.Design;
import com.proactive.webapp.entity.DesignCollect;

public class DesignCollectDAOImpl implements DesignCollectDAO {
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public void insert(DesignCollect designCollect) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO design_collect(designID, memberAccount, createDate) VALUES(?,?,NOW())";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, designCollect.getDid());
			smt.setString(2, designCollect.getAccount());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public void delete(DesignCollect designCollect) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM design_collect WHERE designID=? AND memberAccount=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, designCollect.getDid());
			smt.setString(2, designCollect.getAccount());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}
	
	@Override
	public int countTotal(String account) {
		// TODO Auto-generated method stub
		String sql = "SELECT *,(SELECT COUNT(*) FROM design_report d WHERE a.designID = d.designID HAVING COUNT(*)<10) "
				+ "AS reportCount FROM design_collect a JOIN design e ON a.designID = e.designID "
				+ "WHERE a.memberAccount = ? AND designStatus = 1 HAVING reportCount IS NOT NULL";
		int count = 0;
		try {			
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, account);
			rs = smt.executeQuery();
			rs.beforeFirst();  
			rs.last(); 
			count =rs.getRow();
			
			rs.close();
			smt.close();
			
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return count;
	}

	@Override
	public List<Design> getList(String loginAccount, String account, int start, int size) {
		// TODO Auto-generated method stub
		List<Design> designList = new ArrayList<Design>();
		String sql = "SELECT *,(SELECT COUNT(*) FROM design_comment b WHERE topCommentID IS NULL AND a.designID = b.designID) "
				+ "AS commentCount,(SELECT COUNT(*) FROM design_favorite c WHERE a.designID = c.designID) "
				+ "AS favoriteCount,(SELECT COUNT(*) FROM design_report d WHERE a.designID = d.designID HAVING COUNT(*)<10) "
				+ "AS reportCount,(SELECT COUNT(*) FROM design_collect e WHERE a.designID = e.designID AND e.memberAccount = ?) "
				+ "AS collect FROM design_collect a JOIN design f ON a.designID = f.designID "
				+ "WHERE a.memberAccount = ? AND designStatus = 1 HAVING reportCount IS NOT NULL "
				+ "ORDER BY a.createDate DESC LIMIT ?,?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, loginAccount);
			smt.setString(2, account);
			smt.setInt(3, start);
			smt.setInt(4, size);
			rs = smt.executeQuery();
			while(rs.next()){
				Design design = new Design();
				design.setId(rs.getInt("designID"));
				design.setTitle(rs.getString("designTitle"));
				design.setImagePath(rs.getString("designImage"));
				design.setIsFile(rs.getInt("isFile"));
				design.setFileName(rs.getString("designFileName"));
				design.setFilePath(rs.getString("designFile"));
				design.setDownloadUrl(design.getFilePath().equals("")?"":("download?t=2&id="+design.getId()));
				design.setFileUrl(rs.getString("designFileUrl"));
				design.setStatus(rs.getInt("designStatus"));
				design.setIsPractice(rs.getInt("isPractice"));
				design.setPracticeClass(design.getIsPractice()==1?"practice":"");
				design.setCid(rs.getInt("courseID"));
				design.setViewCount(rs.getInt("designViewCount"));
				design.setFavoriteCount(rs.getInt("favoriteCount"));
				design.setCommentCount(rs.getInt("commentCount"));
				design.setCollectFlag(rs.getInt("collect")==0?false:true);
				design.setCollectString(design.isCollectFlag()?"已收藏":"收藏");
				design.setCollectString1(design.isCollectFlag()?"none":"inline");
				design.setCollectString2(design.isCollectFlag()?"inline":"none");
				design.setCollectClass(design.isCollectFlag()?"topfocusCollect":"");
				designList.add(design);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return designList;
	}

}
